package cn.zh.storage.mapper;

import cn.zh.storage.entity.Storage;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * @author zh
 */
@Mapper
public interface StorageMapper extends BaseMapper<Storage> {

    /**
     * 获取库存
     * @param commodityCode 商品编号
     * @return
     */
    @Select("SELECT id,commodity_code,count FROM tcc_storage WHERE commodity_code = #{commodityCode}")
    Storage findByCommodityCode(@Param("commodityCode") String commodityCode);

    /**
     * 扣减库存
     * @param commodityCode 商品编号
     * @param count  要扣减的库存
     * @return
     */
    @Update("UPDATE tcc_storage SET count = count - #{count}, freeze_count = freeze_count + #{count} WHERE commodity_code = #{commodityCode}")
    int reduceStorage(@Param("commodityCode") String commodityCode,@Param("count") Integer count);

    @Update("UPDATE tcc_storage SET freeze_count = freeze_count - #{count}  WHERE commodity_code = #{commodityCode}")
    void reduceFreezeCount(String commodityCode, Integer count);

    @Update("UPDATE tcc_storage SET count = count + #{count}, freeze_count = freeze_count - #{count}  WHERE commodity_code = #{commodityCode}")
    void unFreezeCount(String commodityCode, Integer count);
}
